package com.itbaizhan.shopping_manager_api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.shopping_common.pojo.Admin;
import com.itbaizhan.shopping_common.pojo.Permission;
import com.itbaizhan.shopping_common.result.BaseResult;
import com.itbaizhan.shopping_common.service.AdminService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.bouncycastle.cms.RecipientId.password;

/**
 * 后台管理员
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
	
	@DubboReference
	private AdminService adminService;
	
	@Autowired
	private PasswordEncoder passwordEncoder;
	
	
	/**
	 * 新增管理员
	 *
	 * @param admin 管理员对象
	 * @return 执行结果
	 */
	@PostMapping("/add")
	public BaseResult add(@RequestBody Admin admin) {
		//对新增的密码进行加密处理
		String password = passwordEncoder.encode(admin.getPassword());
		admin.setPassword(password);
		adminService.add(admin);
		return BaseResult.ok();
	}
	
	/**
	 * 修改管理员
	 *
	 * @param admin 管理员对象
	 * @return 执行结果
	 */
	@PutMapping("/update")
	public BaseResult update(@RequestBody Admin admin) {
		//判断是否修改密码，修改密码需要对密码进行加密处理
		if (StringUtils.hasText(admin.getPassword())) {
			admin.setPassword(passwordEncoder.encode(admin.getPassword()));
		}
		adminService.update(admin);
		return BaseResult.ok();
	}
	
	/**
	 * 删除管理员
	 *
	 * @param aid 管理员id
	 * @return 执行结果
	 */
	@DeleteMapping("/delete")
	public BaseResult delete(Long aid) {
		adminService.delete(aid);
		return BaseResult.ok();
	}
	
	/**
	 * 根据id查询管理员
	 *
	 * @param aid
	 * @return 查询到的管理员
	 */
	@GetMapping("/findById")
	public BaseResult<Admin> findById(Long aid) {
		Admin admin = adminService.findById(aid);
		return BaseResult.ok(admin);
	}
	
	/**
	 * 分页查询管理员
	 *
	 * @param page
	 * @param size
	 * @return
	 */
	@GetMapping("/search")
	@PreAuthorize("hasAnyAuthority('/admin/search')")
	public BaseResult<Page<Admin>> search(int page, int size) {
		Page<Admin> search = adminService.search(page, size);
		return BaseResult.ok(search);
	}
	
	/**
	 * 给管理员分配角色
	 *
	 * @param aid  管理员id
	 * @param rids 角色id数组
	 * @return 执行结果
	 */
	@PutMapping("/updateRoleToAdmin")
	public BaseResult updateRoleToAdmin(@RequestParam("aid") Long aid, @RequestParam("rids") Long[] rids) {
		adminService.updateRoleToAdmin(aid, rids);
		return BaseResult.ok();
	}
	
	/**
	 * 获取登录的管理员名
	 *固定写法
	 * @return
	 */
	@GetMapping("/getUsername")
	public BaseResult<String> getUsername() {
		//1.获取会话对象
		SecurityContext context = SecurityContextHolder.getContext();
		//2.获取认证对象
		Authentication authentication = context.getAuthentication();
		//3,获取登录用户信息
		UserDetails userDetails = (UserDetails) authentication.getPrincipal();
		
		return BaseResult.ok(userDetails.getUsername());
		
	}
	
	
}
